﻿using System;
using GpuHud;
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.Profiling;

public struct PlayerNameAlterContext
{
    public bool refreshColor;
    public Color color;
}

public class GpuPlayerNameEntity : GpuHudEntityBase
{
    public override GpuHudEntityType GetEntityType()
    {
        return GpuHudEntityType.PlayerName;
    }
    
    #region 参数
    Vector3 initPos;
    #endregion

    protected override void LateInit()
    {
    }

    protected override void LateUpdate()
    {
    }

    /// <summary>
    /// 为了减少重构次数,设置位置后更新位置只需要更新Offset即可
    /// </summary>
    /// <param name="pos"></param>
    public void SetOriginPosition(Vector3 pos)
    {
        initPos = pos;
    }

    /// <summary>
    /// 初次设置Pos后续都使用偏移,这个更新发生在Update阶段,dirty的更新发生在LateUpdate
    /// </summary>
    /// <param name="pos"></param>
    public void SetPosition(Vector3 pos,bool force = false)
    {
        m_gpuHudAccessor.SetTextPosition(pos,force);
    }

    public void SetColor(Color color)
    {
        m_gpuHudAccessor.SetTextColor(color);
    }
    
    public void SetScale(float scale)
    {
        m_gpuHudAccessor.SetTextScale(scale);
    }

    public void SetVisible(bool visible)
    {
        if (visible && !string.IsNullOrEmpty(GpuHudAccessor.TextContent))
        {
            m_gpuHudAccessor.SetOnScreen();
        }
        else
        {
            m_gpuHudAccessor.SetTextOffScreen();
        }
    }

    /// <summary>
    /// SetName必须重绘数据,这个没法解决
    /// 但是第一次OnPlay之前可以拒绝rebuild
    /// </summary>
    /// <param name="nickName"></param>
    public void SetName(string nickName,PlayerNameAlterContext context,bool forceRebuild)
    {
        GpuHudAccessor.AlterTextStyleAccesor(nickName, forceRebuild);
        if (forceRebuild)
        {
            if (context.refreshColor)
            {
                m_gpuHudAccessor.SetTextColor(context.color);
            }
            m_gpuHudAccessor.SetOnScreen();
        }
        else
        {
            if (!String.IsNullOrEmpty(nickName))
            {
                m_gpuHudAccessor.SetOnScreen();
            }else m_gpuHudAccessor.SetOffScreen();
        }
    }

    protected override void PrePlay()
    {
        m_gpuHudAccessor.Position = initPos;
    }

    protected override void LatePlay()
    {
    }

    protected override void PreBackPool()
    {
        
    }

    protected override void LateOutPool()
    {
        
    }
}
